package com.sysu.minsysu.xyxw;

import com.sysu.minsysu.R;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

public class VideoList extends ExpandableListActivity{

	ExpandableListAdapter mAdapter;
	ExpandableListView mListView;
	
	String[] mParentNames;
	String[][] mChildrenNames;
	String[][] mVideoPaths;
	int[] mLength;
	Intent mIntent;
	
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		mListView = getExpandableListView();
		mListView.setBackgroundResource(R.drawable.background);
		mListView.setDrawingCacheEnabled(true);
		mListView.setCacheColorHint(0);
		mIntent = getIntent();
		setupViews(mIntent);
		mAdapter = new MyExpandableListAdapter();
		setListAdapter(mAdapter);
	}
	
	void setupViews( Intent intent ){
		Bundle bundle = intent.getExtras();
		mParentNames = bundle.getStringArray("names");
		mLength = bundle.getIntArray("length");
		//mVideoPaths = (String[][])bundle.getSerializable("paths");
		mChildrenNames = new String[mParentNames.length][];
		for( int i=0; i<mParentNames.length; i++ ){
			mChildrenNames[i] = new String[ mLength[i] ];
			for( int j=0; j<mLength[i]; j++ ){
				mChildrenNames[i][j] = "第" + (j+1) + "课";
			}
		}
		
		String server= "rtsp://222.200.180.213/";
		mVideoPaths = new String[ mParentNames.length][];
		for( int i=0; i<mVideoPaths.length; i++ ){
			mVideoPaths[i] = new String[ mLength[i] ];
			for( int j=0; j< mLength[i]; j++ ){
				mVideoPaths[i][j] = server + "justice" + (j%4+1) + ".mp4";
			}
		}
		
		mListView.setOnChildClickListener( new ExpandableListView.OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				Intent intent = new Intent(VideoList.this, VideoPlayView.class);
				intent.putExtra("url", mVideoPaths[groupPosition][childPosition]);
				Log.e("VideoList", mVideoPaths[groupPosition][childPosition]);
				startActivity(intent);
				return true;
			}
		});
	}
	
	
	public class MyExpandableListAdapter extends BaseExpandableListAdapter{

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return mChildrenNames[groupPosition][childPosition];
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
		
		 public TextView getGenericView() {
	            // Layout parameters for the ExpandableListView
	            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
	                    ViewGroup.LayoutParams.FILL_PARENT, 64);

	            TextView textView = new TextView(VideoList.this);
	            textView.setLayoutParams(lp);
	            // Center the text vertically
	            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
	            // Set the text starting position
	            textView.setPadding(36, 0, 0, 0);
	            return textView;
	        }

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setTextColor(Color.BLACK);
			textView.setTextSize(19);
			textView.setText( getChild(groupPosition, childPosition).toString());
			return textView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mChildrenNames[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return mParentNames[groupPosition];
		}

		@Override
		public int getGroupCount() {
			return mParentNames.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setTextColor(Color.BLACK);
			textView.setTextSize(22);
			textView.setText( getGroup(groupPosition).toString() );
			return textView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
	}
}
